/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.perutaxi.datos;

import com.perutaxi.entidades.eCliente;
import com.perutaxi.entidades.eEmpresaTaxista;
import com.perutaxi.entidades.eTaxistaAuto;
import com.perutaxi.entidades.eTaxistaServicio;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author PeruTaxi
 */
public class dTaxistaServicio implements Serializable {

    public static eTaxistaServicio inicializar() {
        eTaxistaServicio taxServicio = new eTaxistaServicio();
        taxServicio.setTaxista(new eEmpresaTaxista());
        taxServicio.setCliente(new eCliente());
        taxServicio.setOrigen("");
        taxServicio.setDestino("");
        taxServicio.setComentario("");
        taxServicio.setCosto(BigDecimal.ZERO);
        taxServicio.setCantPasajeros(0L);

        return taxServicio;
    }

    public static eTaxistaServicio obtenerPorId(Long id) throws Exception {
        eTaxistaServicio taxServicio = new eTaxistaServicio();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_SERVICIO WHERE ID = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                taxServicio.setId(rs.getLong(1));
                taxServicio.setTaxista(dEmpresaTaxista.obtenerPorId(rs.getLong(2)));
                taxServicio.setCliente(dCliente.obtenerPorId(rs.getLong(3)));
                taxServicio.setFechaServicio(rs.getTimestamp(4));
                taxServicio.setOrigen(rs.getString(5));
                taxServicio.setDestino(rs.getString(6));
                taxServicio.setComentario(rs.getString(7));
                taxServicio.setCosto(rs.getBigDecimal(8));
                taxServicio.setCantPasajeros(rs.getLong(9));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return taxServicio;
    }

    public static List<eTaxistaServicio> obtenerPorIdTaxista(Long id) throws Exception {
        List<eTaxistaServicio> lista = new ArrayList<eTaxistaServicio>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_SERVICIO WHERE TAXISTA = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eTaxistaServicio taxServicio = new eTaxistaServicio();
                taxServicio.setId(rs.getLong(1));
                taxServicio.setTaxista(dEmpresaTaxista.obtenerPorId(rs.getLong(2)));
                taxServicio.setCliente(dCliente.obtenerPorId(rs.getLong(3)));
                taxServicio.setFechaServicio(rs.getTimestamp(4));
                taxServicio.setOrigen(rs.getString(5));
                taxServicio.setDestino(rs.getString(6));
                taxServicio.setComentario(rs.getString(7));
                taxServicio.setCosto(rs.getBigDecimal(8));
                taxServicio.setCantPasajeros(rs.getLong(9));
                lista.add(taxServicio);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static List<eTaxistaServicio> obtenerTodos() throws Exception {
        List<eTaxistaServicio> lista = new ArrayList<eTaxistaServicio>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_SERVICIO";
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eTaxistaServicio taxServicio = new eTaxistaServicio();
                taxServicio.setId(rs.getLong(1));
                taxServicio.setTaxista(dEmpresaTaxista.obtenerPorId(rs.getLong(2)));
                taxServicio.setCliente(dCliente.obtenerPorId(rs.getLong(3)));
                taxServicio.setFechaServicio(rs.getTimestamp(4));
                taxServicio.setOrigen(rs.getString(5));
                taxServicio.setDestino(rs.getString(6));
                taxServicio.setComentario(rs.getString(7));
                taxServicio.setCosto(rs.getBigDecimal(8));
                taxServicio.setCantPasajeros(rs.getLong(9));
                lista.add(taxServicio);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static boolean guardar(eTaxistaServicio taxServicio) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "INSERT INTO TAXISTA_SERVICIO VALUES (?,?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, taxServicio.getId());
            ps.setLong(2, taxServicio.getTaxista().getId());
            ps.setLong(3, taxServicio.getCliente().getId());
            ps.setTimestamp(4, taxServicio.getFechaServicio());
            ps.setString(5, taxServicio.getOrigen().toUpperCase());
            ps.setString(6, taxServicio.getDestino().toUpperCase());
            ps.setString(7, taxServicio.getComentario().toUpperCase());
            ps.setBigDecimal(8, taxServicio.getCosto());
            ps.setLong(9, taxServicio.getCantPasajeros());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        } finally {
            cnx.close();
        }

        return retorno;
    }

    public static boolean actualizar(eTaxistaServicio taxServicio) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "UPDATE TAXISTA_SERVICIO SET TAXISTA = ?, CLIENTE = ?, FECHA_SERVICIO = ?, ORIGEN = ?, DESTINO = ?, ";
        query += "COMENTARIO = ?, COSTO = ?, CANT_PASAJERO = ? WHERE ID = ?";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, taxServicio.getTaxista().getId());
            ps.setLong(2, taxServicio.getCliente().getId());
            ps.setTimestamp(3, taxServicio.getFechaServicio());
            ps.setString(4, taxServicio.getOrigen().toUpperCase());
            ps.setString(5, taxServicio.getDestino().toUpperCase());
            ps.setString(6, taxServicio.getComentario().toUpperCase());
            ps.setBigDecimal(7, taxServicio.getCosto());
            ps.setLong(8, taxServicio.getCantPasajeros());
            ps.setLong(9, taxServicio.getId());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        } finally {
            cnx.close();
        }

        return retorno;
    }
}